#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
class MapTT {
public:
	char map[10][10] = {'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'};
};     //init map
class MapNN {
public:
	char map[9][9] = {'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'};
};
class MapFF {
public:
	char map[4][4] = {'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G'};
};
class Player {
public:
	string name;
	int xLocation = 0, yLocation = 0;
	int hp = 500;
};
time_t t;
int fps, fpst;
Player p;
int ttGameLoop();
int nnGameLoop();
int ffGameLoop();
int main() {
	srand(unsigned(t));
	cout << "M A P       G A M E\n";
	usleep(1000000);
	cin >> p.name;
	cin >> fps;
	fpst = int(1000000 / fps);
	int mapSize;
	cin >> mapSize;
	if (mapSize == 10) {
		ttGameLoop();
	} else if (mapSize == 9) {
		nnGameLoop();
	} else {
		ffGameLoop();
	}
	return 0;
}
int ttGameLoop() {
    int score = 0;
	MapTT map1;
	for (unsigned long long i=0;; i++) {                  //game main loop
		score+=i;
		int tempN=rand() % 10,tempM=rand() % 10;
		if (i % 1 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'E';
		}
		if (i % 11 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'S';
		}
		if (i % 4 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'F';
		}
		char fx;
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				if (i == p.xLocation && j == p.yLocation) {
					if (map1.map[i][j] == 'F') {
						p.hp += 50;
						score += 100;
						map1.map[i][j] = 'G';
					} else if (map1.map[i][j] == 'S') {
						p.hp += 100;
						score += 200;
						map1.map[i][j] = 'G';
					} else if (map1.map[i][j] == 'E') {
						p.hp -= (rand() % 200);
						score += 300;
						map1.map[i][j] = 'G';
					}
					for (int i = 0; i <= (7 - p.name.size()); i++) {
						cout << " ";
					}
					cout << p.name;
					continue;
				}
				cout << "       " << map1.map[i][j];
			}
			cout << endl;
		}
		cin >> fx;                               //inputs
		if (int(fx) >= 97) {
			fx -= 32;
		}
		if (fx == 'P') {
			system("cls");
			continue;
		}
		if (fx == 'A') {
			p.yLocation -= 1;
		} else if (fx == 'D') {
			p.yLocation += 1;
		} else if (fx == 'S') {
			p.xLocation += 1;
		} else if (fx == 'W') {
			p.xLocation -= 1;
		} else if (fx == 'C') {
			cin >> fps;
			fpst = int(1000000 / fps);
		} else if (fx == 'F') {
			cout << fps << " " << fpst;
			usleep(2000000);
		} else if (fx == 'T') {
			cout << "HP" << p.hp << endl;
			int anses[3] = {0};
			for (int i = 0; i < 10; i++) {
				for (int j = 0; j < 10; j++) {
					if (map1.map[i][j] == 'E') {
						anses[0]++;
					}
					if (map1.map[i][j] == 'F') {
						anses[1]++;
					}
					if (map1.map[i][j] == 'S') {
						anses[2]++;
					}
				}
			}
			cout << "E" << anses[0] << endl;
			cout << "F" << anses[1] << endl;
			cout << "S" << anses[2] << endl;
			cout << "Your score:" << score << endl;
			cout << "Step:" << i << endl;
			usleep(2000000);
		} else if (fx == 'B') {
			char debuginput;
			cin >> debuginput;
			if (debuginput == 'C') {
				cin >> p.hp;
			} else if (debuginput == 'M') {
				int n, m;
				cin >> n >> m >> map1.map[n][m];
			}
		} else {
			cout << "Error input!\n";
		}
		usleep(fpst);
		system("cls");
		if (p.hp <= 0) {
			cout << "G A M E      O V E R.\n";
			cout << "Your score:" << score << endl;
			cout << "Again?(y/n):";
			char yon;
			cin >> yon;
			if (yon == 'y') {
				system("cls");
				cout << "Watch out,for this time!\n";
				main();                 //restarter
			} else if (yon == 'n') {
				cout << "Thanks for play!\n";         //end of game
				usleep(2000000);
				return 0;
			}
		}
	}
}
int nnGameLoop() {
    int score = 0;
	MapNN map1;
	for (unsigned long long i=0;; i++) {                  //game main loop
		score+=i;
        int tempN=rand() % 9,tempM=rand() % 9;
		if (i % 2 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'E';
		}
		if (i % 7 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'S';
		}
		if (i % 5 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'F';
		}
		char fx;
		for (int i = 0; i < 9; i++) {
			for (int j = 0; j < 9; j++) {
				if (i == p.xLocation && j == p.yLocation) {
					if (map1.map[i][j] == 'F') {
						p.hp += 40;
						score += 75;
						map1.map[i][j] = 'G';
					} else if (map1.map[i][j] == 'S') {
						p.hp += 80;
						score += 125;
						map1.map[i][j] = 'G';
					} else if (map1.map[i][j] == 'E') {
						p.hp -= (rand() % 150);
						score += 200;
						map1.map[i][j] = 'G';
					}
					for (int i = 0; i <= (7 - p.name.size()); i++) {
						cout << " ";
					}
					cout << p.name;
					continue;
				}
				cout << "       " << map1.map[i][j];
			}
			cout << endl;
		}
		cin >> fx;                               //inputs
		if (int(fx) >= 97) {
			fx -= 32;
		}
		if (fx == 'P') {
			system("cls");
			continue;
		}
		if (fx == 'A') {
			p.yLocation -= 1;
		} else if (fx == 'D') {
			p.yLocation += 1;
		} else if (fx == 'S') {
			p.xLocation += 1;
		} else if (fx == 'W') {
			p.xLocation -= 1;
		} else if (fx == 'C') {
			cin >> fps;
			fpst = int(1000000 / fps);
		} else if (fx == 'F') {
			cout << fps << " " << fpst;
			usleep(2000000);
		} else if (fx == 'T') {
			cout << "HP" << p.hp << endl;
			int anses[3] = {0};
			for (int i = 0; i < 9; i++) {
				for (int j = 0; j < 9; j++) {
					if (map1.map[i][j] == 'E') {
						anses[0]++;
					}
					if (map1.map[i][j] == 'F') {
						anses[1]++;
					}
					if (map1.map[i][j] == 'S') {
						anses[2]++;
					}
				}
			}
			cout << "E" << anses[0] << endl;
			cout << "F" << anses[1] << endl;
			cout << "S" << anses[2] << endl;
			cout << "Your score:" << score << endl;
			cout << "Step:" << i << endl;
			usleep(2000000);
		} else if (fx == 'B') {
			char debuginput;
			cin >> debuginput;
			if (debuginput == 'C') {
				cin >> p.hp;
			} else if (debuginput == 'M') {
				int n, m;
				cin >> n >> m >> map1.map[n][m];
			}
		} else {
			cout << "Error input!\n";
		}
		usleep(fpst);
		system("cls");
		if (p.hp <= 0) {
			cout << "G A M E      O V E R.\n";
			cout << "Your score:" << score << endl;
			cout << "Again?(y/n):";
			char yon;
			cin >> yon;
			if (yon == 'y') {
				system("cls");
				cout << "Watch out,for this time!\n";
				main();                 //restarter
			} else if (yon == 'n') {
				cout << "Thanks for play!\n";         //end of game
				usleep(2000000);
				return 0;
			}
		}
	}
}
int ffGameLoop() {
    int score = 0;
	MapFF map1;
	for (unsigned long long i=0;; i++) {                  //game main loop
		score+=i;
		int tempN=rand() % 4,tempM=rand() % 4;
		if (i % 3 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'E';
		}
		if (i % 8 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'S';
		}
		if (i % 6 == 0 && map1.map[tempN][tempM] == 'G') {
			map1.map[tempN][tempM] = 'F';
		}
		char fx;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (i == p.xLocation && j == p.yLocation) {
					if (map1.map[i][j] == 'F') {
						p.hp += 30;
						score += 50;
						map1.map[i][j] = 'G';
					} else if (map1.map[i][j] == 'S') {
						p.hp += 60;
						score += 75;
						map1.map[i][j] = 'G';
					} else if (map1.map[i][j] == 'E') {
						p.hp -= (rand() % 100);
						score += 125;
						map1.map[i][j] = 'G';
					}
					for (int i = 0; i <= (7 - p.name.size()); i++) {
						cout << " ";
					}
					cout << p.name;
					continue;
				}
				cout << "       " << map1.map[i][j];
			}
			cout << endl;
		}
		cin >> fx;                               //inputs
		if (int(fx) >= 97) {
			fx -= 32;
		}
		if (fx == 'P') {
			system("cls");
			continue;
		}
		if (fx == 'A') {
			p.yLocation -= 1;
		} else if (fx == 'D') {
			p.yLocation += 1;
		} else if (fx == 'S') {
			p.xLocation += 1;
		} else if (fx == 'W') {
			p.xLocation -= 1;
		} else if (fx == 'C') {
			cin >> fps;
			fpst = int(1000000 / fps);
		} else if (fx == 'F') {
			cout << fps << " " << fpst;
			usleep(2000000);
		} else if (fx == 'T') {
			cout << "HP" << p.hp << endl;
			int anses[3] = {0};
			for (int i = 0; i < 4; i++) {
				for (int j = 0; j < 4; j++) {
					if (map1.map[i][j] == 'E') {
						anses[0]++;
					}
					if (map1.map[i][j] == 'F') {
						anses[1]++;
					}
					if (map1.map[i][j] == 'S') {
						anses[2]++;
					}
				}
			}
			cout << "E" << anses[0] << endl;
			cout << "F" << anses[1] << endl;
			cout << "S" << anses[2] << endl;
			cout << "Your score:" << score << endl;
			cout << "Step:" << i << endl;
			usleep(2000000);
		} else if (fx == 'B') {
			char debuginput;
			cin >> debuginput;
			if (debuginput == 'C') {
				cin >> p.hp;
			} else if (debuginput == 'M') {
				int n, m;
				cin >> n >> m >> map1.map[n][m];
			}
		} else {
			cout << "Error input!\n";
		}
		usleep(fpst);
		system("cls");
		if (p.hp <= 0) {
			cout << "G A M E      O V E R.\n";
			cout << "Your score:" << score << endl;
			cout << "Again?(y/n):";
			char yon;
			cin >> yon;
			if (yon == 'y') {
				system("cls");
				cout << "Watch out,for this time!\n";
				main();                 //restarter
			} else if (yon == 'n') {
				cout << "Thanks for play!\n";         //end of game
				usleep(2000000);
				return 0;
			}
		}
	}
}
